/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mancj.slideup.slice;

import com.mancj.slideup.ResourceTable;

import com.mancj.slideup.Gravity;
import com.mancj.slideup.SlideUp;
import com.mancj.slideup.SlideUpBuilder;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ListDialog;
import ohos.agp.window.dialog.ToastDialog;

/**
 * Slide start view ability slice
 */
public class SlideStartViewAbilitySlice extends AbilitySlice {
    /**
     * The constant Item
     */
    private String[] item = new String[]{"SlideUp Sample", "SlideDown Sample", "SlideStart Sample", "SlideEnd Sample"};
    /**
     * The constant Slide up
     */
    private SlideUp slideUp;
    /**
     * The constant Dim
     */
    private Component dim;
    /**
     * The constant Slider view
     */
    private Component sliderView;
    /**
     * The constant Fab
     */
    private Component fab;

    /**
     * On start *
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_slide_start_view);

        sliderView = findComponentById(ResourceTable.Id_slideView);
        dim = findComponentById(ResourceTable.Id_dim);
        fab = findComponentById(ResourceTable.Id_fab);
        slideUp = new SlideUpBuilder(sliderView)
                .withListeners(new SlideUp.Listener.Events() {
                    @Override
                    public void onSlide(float percent) {
                        dim.setAlpha(1 - (percent / 100));
                        if (fab.isComponentDisplayed() && percent < 100) {
                            fab.setVisibility(Component.HIDE);
                        }
                    }

                    @Override
                    public void onVisibilityChanged(int visibility) {
                        if (visibility == Component.HIDE) {
                            fab.setVisibility(Component.VISIBLE);
                        }
                    }
                })
                .withStartGravity(Gravity.START)
                .withLoggingEnabled(true)
                .withGesturesEnabled(true)
                .withStartState(SlideUp.State.HIDDEN)
                .withSlideFromOtherView(findComponentById(ResourceTable.Id_rootView))
                .build();
        sliderView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ToastDialog toastDialog = new ToastDialog(getContext());
                toastDialog.setText("click").show();
            }
        });
        fab.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                slideUp.show();
            }
        });
        initChangeView();
    }

    /**
     * Init change view
     */
    private void initChangeView() {
        findComponentById(ResourceTable.Id_change_view).setClickedListener(new Component.ClickedListener() {
            void gotoAbility(int position) {
                switch (position) {
                    case 0:
                        present(new SlideUpViewAbilitySlice(), new Intent());
                        break;
                    case 1:
                        present(new SlideDownViewAbilitySlice(), new Intent());
                        break;
                    case 2:
                        present(new SlideStartViewAbilitySlice(), new Intent());
                        break;
                    case 3:
                        present(new SlideEndViewAbilitySlice(), new Intent());
                        break;
                }
            }

            @Override
            public void onClick(Component component) {
                ListDialog listDialog = new ListDialog(SlideStartViewAbilitySlice.this);
                listDialog.setSingleSelectItems(item, 2);
                listDialog.setOnSingleSelectListener(new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int position) {
                        gotoAbility(position);
                        listDialog.hide();
                    }
                });
                listDialog.setButton(0, "取消", new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int position) {
                        listDialog.destroy();
                    }
                });
                listDialog.show();
            }
        });
    }

    /**
     * On active
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * On foreground *
     *
     * @param intent intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
